﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;


namespace HongYeSystemWebApi.Templet.Entity.ApiResult
{

    /// <summary>
    /// 携带Data 的 API返回对象
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class ApiResult<TData> : ApiResult
    {
        private TData _data = default;
        /// <summary>
        /// 构造方法
        /// </summary>
        public ApiResult()
            : base()
        {
            _data = default;
        }
        /// <summary>
        /// 操作结果业务数据
        /// </summary>
        public TData Data
        {
            get
            {
                if (typeof(TData).BaseType == typeof(IEnumerator) && _data == null)
                    _data = Activator.CreateInstance<TData>();

                return _data;
            }
            set { _data = value; }
        }
    }
    ///	日期：2021/8/5 11:18:11 
    ///	描述: ApiResult            
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int StatusCode { get; set; }

        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Status { get; set; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        public ApiResult(string msg = "")
        {
            this.StatusCode = 200;
            this.Status = true;
            this.Message = msg;
        }

        /// <summary>
        /// 返回True
        /// </summary>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiResult<TData> True<TData>(TData data) 
        {
            return new ApiResult<TData>() {
                Data = data,
                StatusCode = 200,
                Status = true,
                Message="",
            };
        }
        /// <summary>
        /// 返回True
        /// </summary>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiResult True(string message = "")
        {
            return new ApiResult()
            {
                StatusCode = 200,
                Status = true,
                Message = message,
            };
        }
        /// <summary>
        /// 返回False
        /// </summary>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiResult  False (string data )
        {
            return new ApiResult()
            {
                StatusCode = 500,
                Status = false,
                Message = data,
            };
        }
        /// <summary>
        /// 返回False
        /// </summary>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiResult False(string data,int StatusCode)
        {
            return new ApiResult()
            {
                StatusCode = StatusCode,
                Status = false,
                Message = data,
            };
        }
        public class SuccessResponse<T> : ApiResult where T : new()
        {
            public SuccessResponse(T result)
            {
                StatusCode = 200;
                Status = true;
                Result = result;
            }
            /// <summary>
            /// 返回数据
            /// </summary>
            public T Result { get; set; }

        }
    }
}
